package mx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import mx.entity.Employee;
import mx.entity.PageBean;
import mx.entity.Ticket;
import mx.entity.TicketOrder;
import mx.service.TicketOrderService;
import mx.service.TicketService;
import mx.utils.DateJsonValueProcessor;
import mx.utils.MakeOrderNum;
import mx.utils.ResponseUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * 票controller
 * 
 * @author root
 *
 */
@Controller
@RequestMapping("/ticket")
public class TicketController {

	@Resource
	private TicketService ticketService;

	@Resource
	private TicketOrderService ticketOrderService;

	@RequestMapping("/list")
	public String list(@RequestParam(value = "page", required = false) String page,
			@RequestParam(value = "rows", required = false) String rows, Employee s_employee,
			HttpServletResponse response) throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Ticket> ticketList = ticketService.list(map);
		long totatl = ticketService.getTotal(map);
		JSONObject result = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd hh:mm:ss"));
		JSONArray jsonArray = JSONArray.fromObject(ticketList, jsonConfig);
		result.put("rows", jsonArray);
		result.put("total", totatl);
		ResponseUtil.write(response, result);
		return null;
	}

	/**
	 * 根据订单出票
	 * 
	 * @return
	 */
	@RequestMapping("/byOrderList/{orderId}")
	public ModelAndView byOrderList(@PathVariable("orderId") Integer orderId) throws Exception {
		ModelAndView modelAndView = null;
		if (orderId != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("order_Id", orderId);
			List<Ticket> ticketList = ticketService.list(map);// 查票的list
			System.out.println(ticketList.size());
			modelAndView = new ModelAndView();
			modelAndView.addObject("ticketList", ticketList);
			modelAndView.addObject("outTicketPage", "ticketManage/outTicketPage/outTicketPrintPage.jsp");
			modelAndView.setViewName("admin/transferPage");
		}
		return modelAndView;
	}

	/**
	 * 根据现金出票
	 * 
	 * @return
	 */
	@RequestMapping("/saveByCash")
	public String saveByCash(Ticket ticket, HttpServletResponse response) throws Exception {
		int resultTotal = 0;
		TicketOrder ticketOrder = new TicketOrder();
		if (ticket.getId() == null) {
			Integer count1 = ticket.getAdultCount();
			Integer count2 = ticket.getChildCount();
			Double price1 = ticket.getAdultPrice();
			Double price2 = ticket.getChildPrice();
			if (count1 != null && count2 != null && price1 != null && price2 != null) {
				Double totalPrice = count1 * price1 + count2 * price2;
				ticketOrder.setTotalPrice(totalPrice);
				ticket.setTotalPrice(totalPrice);
				if ((count1 + count2) > 1) {
					ticketOrder.setType("团队");
				} else {
					ticketOrder.setType("个人");
				}
			}
			ticketOrder.setOrderNumber(MakeOrderNum.makeOrderNum());
			ticketOrderService.add(ticketOrder);// 添加订单
			ticket.setTicketOrder(ticketOrder);
			resultTotal = ticketService.add(ticket);// 出票
		} else {
			// resultTotal = ticketService.update(ticket);// 更新票信息
		}
		JSONObject result = new JSONObject();
		if (resultTotal > 0) {
			result.put("success", true);
			result.put("data", ticketOrder.getId());
		} else {
			result.put("success", false);
		}
		ResponseUtil.write(response, result);
		return null;
	}
}
